package com.woniuxy.microgoldservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.microgoldservice.bean.GoldOrderDAO;
import com.woniuxy.microgoldservice.bean.GoldPriceInfo;
import com.woniuxy.microgoldservice.mapper.GoldAccumulateMapper;
import com.woniuxy.microgoldservice.mapper.GoldPriceMapper;
import com.woniuxy.microgoldservice.mapper.GoldPurchaseMapper;
import com.woniuxy.microgoldservice.service.IGoldpriceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hongbin
 * @date 2021-04-07 16:42
 * Description： 黄金价格
 */
@Service
@Slf4j
public class GoldpriceServiceImpl implements IGoldpriceService {
    @Resource
    private GoldPriceMapper goldPriceMapper;

    @Resource
    private GoldAccumulateMapper goldAccumulateMapper;

    @Resource
    private IGoldpriceService goldpriceService;

    @Resource
    private GoldPurchaseMapper goldPurchaseMapper;

    /**
     * @Author: hongbin
     * @Description: 查询历史金价
     * @DateTime: 20:29 2021/4/15
     * @Params: []
     * @Return java.lang.String
     */
    @Override
    public ResultBean selectAll() {
        List<GoldPriceInfo> goldPriceInfos = goldPriceMapper.selectAll();

        return ResultBean.builder().code(200).msg("OK").data(goldPriceInfos).build();
    }

    /**
     * @Author: hongbin
     * @Description: 插入金价
     * @DateTime: 20:29 2021/4/15
     * @Params: [goldPriceInfo]
     * @Return int
     */

    @Override
    public int insert(GoldPriceInfo goldPriceInfo) {
        return goldPriceMapper.insert(goldPriceInfo);
    }

    /**
     * @Author: hongbin
     * @Description: 更新金价
     * @DateTime: 20:29 2021/4/15
     * @Params: [goldPriceInfo]
     * @Return int
     */
    @Override
    public int update(GoldPriceInfo goldPriceInfo) {
        return goldPriceMapper.update(goldPriceInfo);
    }

    /**
     * @Author: hongbin
     * @Description: 影藏未上架的黄金产品金价
     * @DateTime: 19:52 2021/4/15
     * @Params: [id]
     * @Return int
     */
    @Override
    public int delete(Long proId) {
        return goldPriceMapper.delete(proId);
    }

    /**
     * @Author: hongbin
     * @Description: 查询当天黄金金价
     * @DateTime: 19:52 2021/4/15
     * @Params: []
     * @Return java.util.List<com.woniuxy.microgoldservice.bean.GoldPriceInfo>
     */
    @Override
    public List<GoldPriceInfo> selectToday() {
        Long proId = goldAccumulateMapper.selectProId();
        return goldPriceMapper.selectTodayGoldPrice(proId);
    }

    /**
     * @Author: hongbin
     * @Description: 查询近一个月黄金金价
     * @DateTime: 19:51 2021/4/15
     * @Params: []
     * @Return java.util.List<com.woniuxy.microgoldservice.bean.GoldPriceInfo>
     */
    @Override
    public List<GoldPriceInfo> selectOneMonth(Long proId) {
//        Long proId = goldAccumulateMapper.selectProId();
        return goldPriceMapper.selectOneMonth(proId);
    }

    /**
     * @Author: hongbin
     * @Description: 查询半年黄金价格
     * @DateTime: 19:51 2021/4/15
     * @Params: []
     * @Return java.util.List<com.woniuxy.microgoldservice.bean.GoldPriceInfo>
     */
    @Override
    public List<GoldPriceInfo> selectHalfYear(Long proId) {
        return goldPriceMapper.selectHalfYear(proId);
    }

    /**
     * @Author: hongbin
     * @Description: 查询一年内黄金价
     * @DateTime: 19:50 2021/4/15
     * @Params: []
     * @Return java.util.List<com.woniuxy.microgoldservice.bean.GoldPriceInfo>
     */
    @Override
    public List<GoldPriceInfo> selectOneYear(Long proId) {
        return goldPriceMapper.selectOneYear(proId);
    }

    /**
     * @Author: hongbin
     * @Description: 查询最新时刻的黄金价格
     * @DateTime: 19:49 2021/4/15
     * @Params: []
     * @Return java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> selectTodayPrice() {
        Long pid = goldPriceMapper.selectProId();
        GoldPriceInfo price = goldPriceMapper.selectYesterdayReferencePrice(pid);
        HashMap<String, Object> map = new HashMap<>();
        map.put("proId", pid);
        map.put("price", price.getPrice());
        return map;
    }

    /**
     * @Author: hongbin
     * @Description: 获取当前金价
     * @DateTime: 19:45 2021/4/15
     * @Params: []
     * @Return java.lang.Double
     */
    @Override
    public Double getNowGoldPrice() {
        return goldPriceMapper.getNowGoldPrice().getPrice();
    }

    /**
     * @Author: hongbin
     * @Description:
     * @DateTime: 19:42 2021/4/15
     * @Params: [userName]
     * @Return java.lang.Double
     */
    @Override
    public Double getNowARP(String userName) {
        return goldPriceMapper.getNowARP(userName);
    }

    /**
     * @Author: hongbin
     * @Description: 统计昨日收益，和总收益
     * @DateTime: 19:33 2021/4/15
     * @Params: []
     * @Return int
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int setEarnings() {
        Long proId = goldPriceMapper.selectProId();

        //获取去昨日收盘金价
        GoldPriceInfo yesterdayPrice = goldPriceMapper.getYesterdayPrice(proId);
        //设置昨日利息收益（累积收益，包含赎回了的收益） #{yesterdayPrice}*arp/100/365
        Double arp = goldAccumulateMapper.getARP(proId);
        BigDecimal yp = new BigDecimal(yesterdayPrice.getPrice());
        BigDecimal arpData = new BigDecimal(arp);
        //计算收益率
        BigDecimal earnings = yp.multiply(arpData).divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(365), 4, BigDecimal.ROUND_HALF_UP);

        //设置昨日利息收益
        goldPriceMapper.setEarnings(earnings.doubleValue());
        //设置昨日收益
        BigDecimal todayGoldPrice = new BigDecimal(goldPriceMapper.getTodayGoldPrice(proId).getPrice());
        goldPriceMapper.setTodayEarnings(todayGoldPrice);
        List<GoldOrderDAO> goldOrderDAOS = goldPurchaseMapper.selectAllCost();

        if(goldOrderDAOS.size()==0){
            return 0;
        }
        int i1 = goldPriceMapper.setYesterdayEarnings(goldOrderDAOS);
        log.info("修改了昨日收益条数{}",i1);

        //设置持有收益
        goldPriceMapper.setHoldPrice();

        //设置累积收益
        goldPriceMapper.setTotalEarnings();

        //计算总资产
        Integer i = goldPurchaseMapper.setMonetary(todayGoldPrice);

        //计算持有收益率 （基金的最新市值 + 分红收到的现金 ） / 投入本金 - 1
        goldPurchaseMapper.updateHoldingGains(goldOrderDAOS);








//        Long proId = goldPriceMapper.selectProId();
//
//        //获取去昨日收盘金价
//        GoldPriceInfo yesterdayPrice = goldPriceMapper.getYesterdayPrice(proId);
//        //设置昨日利息收益（累积收益，包含赎回了的收益） #{yesterdayPrice}*arp/100/365
//        Double arp = goldAccumulateMapper.getARP(proId);
//        BigDecimal yp = new BigDecimal(yesterdayPrice.getPrice());
//        BigDecimal arpData = new BigDecimal(arp);
//        //计算收益
//        BigDecimal earnings = yp.multiply(arpData).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(365), 2, BigDecimal.ROUND_HALF_UP);
//        //设置昨日利息收益
//        goldPriceMapper.setEarnings(earnings.doubleValue());
//        //设置昨日收益
//        BigDecimal todayGoldPrice = goldPriceMapper.getTodayGoldPrice(proId);
//        goldPriceMapper.setTodayEarnings(todayGoldPrice);
//        List<GoldOrderDAO> goldOrderDAOS = goldPurchaseMapper.selectAllCost();
//        int i1 = goldPriceMapper.setYesterdayEarnings(goldOrderDAOS);
//        log.info("修改了昨日收益条数{}",i1);
//
//        //设置持有收益
//        goldPriceMapper.setHoldPrice();
//
//        //设置累积收益
//        goldPriceMapper.setTotalEarnings();
//
//        //计算总资产
//        int i = goldPurchaseMapper.setMonetary(todayGoldPrice);
//
//        //计算持有收益率 （基金的最新市值 + 分红收到的现金 ） / 投入本金 - 1
//        goldPurchaseMapper.updateHoldingGains(goldOrderDAOS);
        return i;
    }

    public int setTotalEarnings() {
        return goldPriceMapper.setTotalEarnings();
    }

    /**
     * @Author: hongbin
     * @Description: 零点时分插入的黄金价格
     * @DateTime: 19:31 2021/4/15
     * @Params: [goldPriceInfo]
     * @Return int
     */
    @Override
    public int insertZeroTime(GoldPriceInfo goldPriceInfo) {
        return goldPriceMapper.insertZeroTime(goldPriceInfo);
    }

    /**
     * @Author: hongbin
     * @Description: 获取金价走势图所有时段的数据
     * @DateTime: 19:29 2021/4/15
     * @Params: []
     * @Return java.lang.String
     */
    @Override
    public ResultBean selectChart() {
        //每十分钟的金价
        List<GoldPriceInfo> hour = goldpriceService.selectToday();

        Long proId1 = goldAccumulateMapper.selectProId();

        ArrayList<Double> hp = new ArrayList<>();
        ArrayList<String> ht = new ArrayList<>();
        for (GoldPriceInfo goldPriceInfo : hour) {
            hp.add(goldPriceInfo.getPrice());
            ht.add(goldPriceInfo.getCreateTime().substring(11, 16));
        }
        HashMap<String, Object> hourMap = new HashMap<>();

        //日期
        hourMap.put("categories", ht);

        ArrayList<Object> hMoneyObj = new ArrayList<>();
        HashMap<String, Object> hpMap = new HashMap<>();
        hpMap.put("name", "绿色");
        hpMap.put("data", hp);
        hMoneyObj.add(hpMap);
        //金价
        hourMap.put("series", hMoneyObj);


        //近一个月的金价
        List<GoldPriceInfo> oneMonth = goldpriceService.selectOneMonth(proId1);
        ArrayList<Double> mp = new ArrayList<>();
        ArrayList<String> mt = new ArrayList<>();
        for (GoldPriceInfo info : oneMonth) {
            mp.add(info.getPrice());
            mt.add(info.getCreateTime().substring(5, 10));
        }
        HashMap<String, Object> monthMap = new HashMap<>();
        //日期

        monthMap.put("categories", mt);
        //金价
        ArrayList<Object> oneMonthPrices = new ArrayList<>();
        HashMap<Object, Object> ompMap = new HashMap<>();
        ompMap.put("name", "绿色");
        ompMap.put("data", mp);
        oneMonthPrices.add(ompMap);

        monthMap.put("series", oneMonthPrices);


        //近半年的金价
        List<GoldPriceInfo> halfYear = goldpriceService.selectHalfYear(proId1);
        ArrayList<Double> hym = new ArrayList<>();
        ArrayList<String> hyt = new ArrayList<>();
        for (GoldPriceInfo info : halfYear) {
            hym.add(info.getPrice());
            hyt.add(info.getCreateTime().substring(5, 10));
        }
        HashMap<Object, Object> halfYearMap = new HashMap<>();

        //日期

        halfYearMap.put("categories", hyt);
        //金价
        ArrayList<Object> hypList = new ArrayList<>();
        HashMap<String, Object> hypMap = new HashMap<>();
        hypMap.put("name", "绿色");
        hypMap.put("data", hym);
        hypList.add(hypMap);
        halfYearMap.put("series", hypList);


        //近一年的金价
        List<GoldPriceInfo> oneYear = goldpriceService.selectOneYear(proId1);
        ArrayList<Double> ym = new ArrayList<>();
        ArrayList<String> yt = new ArrayList<>();
        for (GoldPriceInfo info : oneYear) {
            ym.add(info.getPrice());
            yt.add(info.getCreateTime().substring(5, 10));
        }
        HashMap<Object, Object> oneYearMap = new HashMap<>();

        //日期
        oneYearMap.put("categories", yt);
        //金价
        ArrayList<Object> oneYearList = new ArrayList<>();
        HashMap<Object, Object> yearMap = new HashMap<>();
        yearMap.put("name", "绿色");
        yearMap.put("data", ym);
        oneYearList.add(yearMap);
        oneYearMap.put("series", oneYearList);

        //查询从项目启动以来，每天的金价走势
        Long proId = goldAccumulateMapper.selectProId();
        List<GoldPriceInfo> allData = goldPriceMapper.selectAllGoldPrice(proId);
        ArrayList<Object> allDataTime = new ArrayList<>();
        for (GoldPriceInfo info : allData) {
            info.setCreateTime(info.getCreateTime().substring(0, 10));
        }


        HashMap<String, Object> map = new HashMap<>();
        map.put("hour", hourMap);
        map.put("oneMonth", monthMap);
        map.put("halfYear", halfYearMap);
        map.put("oneYear", oneYearMap);
        map.put("allData", allData);

        return ResultBean.builder().code(200).msg("OK").data(map).build();
    }
}
